package cn.jeefast.modules.platform.service.impl;

import cn.jeefast.common.entity.MbCommissioner;
import cn.jeefast.common.utils.R;
import cn.jeefast.modules.platform.dao.MbCommissioner1Dao;
import cn.jeefast.modules.platform.service.MbCommissioner1Service;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 医学专员表
 服务实现类
 * </p>
 *
 * @author theodo
 * @since 2020-01-06
 */
@Service
public class MbCommissioner1ServiceImpl extends ServiceImpl<MbCommissioner1Dao, MbCommissioner> implements MbCommissioner1Service {

    @Autowired
    private MbCommissioner1Dao commissioner1Dao;

    /**
     * 添加或修改专员
     * @param commissioner
     * @return
     */
    @Override
    public R addOrSave(MbCommissioner commissioner) {
        if(commissioner.getCommId()==null){
            commissioner.setCommCode(generateCode());
            commissioner1Dao.insert(commissioner);
        }else{
            commissioner1Dao.updateById(commissioner);
        }
        return R.ok();
    }

    @Override
    public List<MbCommissioner> listByProj(Long pid, Integer page, Integer limit) {
        return commissioner1Dao.listByProj(pid,page,limit);
    }

    /**
     * 随机生成6位密码
     *
     * @return
     */
    public static String generateCode() {
        String[] pa = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
                "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
        StringBuffer sb = new StringBuffer("");
        for (int i = 0; i < 6; i++) {
            sb.append(pa[(Double.valueOf(Math.random() * pa.length).intValue())]);
        }
        String[] spe = {"A"};
        sb.append(spe[(Double.valueOf(Math.random() * spe.length).intValue())]);
        sb.append((int)(Math.random()*100));
        return sb.toString();
    }
}
